﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TechnicBlog.Models;

namespace TechnicBlog.Controllers.StudentManager
{
    public class ScoreController : Controller
    {
        //
        // GET: /Score/

        public ActionResult Index(int id)
        {
            DBContext db = new DBContext();
            StudentEntity model = db.Students.Find(id);
            model.Classes = db.Classes.Find(model.S_ClassesId);
            return View(model);
        }

        public string List(int id)
        {
            DBContext db = new DBContext();
            int pageindex = Convert.ToInt32(Request.Form["page"]);
            int pagesize = Convert.ToInt32(Request.Form["rows"]);
            string Term_Num = "";
            if (Request.QueryString["Term_Num"] != null)
                Term_Num = Request.QueryString["Term_Num"].ToString();
            IList<ScoreEntity> list = db.Scores.ToList();
            var result = from u in list
                         orderby u.Term_Num descending
                         where u.Term_Num.Contains(Term_Num)
                         where u.S_Id == id
                         select u;
            List<ScoreEntity> l = (result).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList();
            int total = result.Count();
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            //这里使用自定义日期格式，如果不使用的话，默认是ISO8601格式           
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd";
            //jsonObject是准备转换的对象
            string s = "{\"total\":" + total + ",\"rows\":" + Newtonsoft.Json.JsonConvert.SerializeObject(l, Newtonsoft.Json.Formatting.Indented, timeConverter) + "}";
            db.Dispose();
            return s;
        }

        public ActionResult Create(int id,ScoreEntity model)
        {
            DBContext db = new DBContext();
            StudentEntity stu = db.Students.Find(id);
            model.Student = stu;
            return View(model);
        }

        public ActionResult Add(int id,ScoreEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                model.Student = null;
                model.S_Id = id;
                db.Scores.Add(model);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>恭喜您！新增成功！','info');TreeLink('Score/Index/"+id+"', '学生基本信息列表');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>新增失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Edit(int id)
        {
            DBContext db = new DBContext();
            ScoreEntity model = db.Scores.Find(id);
            model.Student = db.Students.Find(model.S_Id);
            
            db.Dispose();
            if (model == null)
            {
                RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ScoreEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                ScoreEntity score = db.Scores.Find(model.Id);
                score.Student = db.Students.Find(model.S_Id);
                //score.Student.S_Name = model.Student.S_Name;
                UpdateModel(score);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>恭喜您！编辑成功！','info');TreeLink('Score/Index/"+model.S_Id+"', '学生成绩列表');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>编辑失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Delete(int id)
        {
            DBContext db = new DBContext();
            ScoreEntity model = db.Scores.Find(id);
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>要删除的数据不存在，或已被删除！','info'); $('#rptList').datagrid('reload');");
            }
            db.Scores.Remove(model);
            db.SaveChanges();
            db.Dispose();
            return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>删除成功！','info');$('#rptList').datagrid('reload');");
        }
    }
}
